package com.dongdongshop.shiro;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.SellerService;
import com.dongdongshop.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class UserRealm extends AuthorizingRealm {

    @Reference
    private UserService userService;

    //执行授权逻辑
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        //给资源进行授权
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //添加资源的授权字符串
        info.addStringPermission("add");
        return info;
    }

    //执行认证逻辑
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证逻辑");

        //编写shiro判断逻辑判断用户名和密码
        //1.判断用户名
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        TbUser t = userService.getById(token.getUsername());
        if ( t== null){

           /* * 用户名不存在
            * return null值,shiro底层会抛出UnknownAccountException异常
            **/
            return null;
        }
        //2,判断密码
        //1,返回值 2,查询的密码 3,盐 4,用户真实姓名

        return new SimpleAuthenticationInfo(t, t.getPassword(), ByteSource.Util.bytes(t.getSalt()),"");

    }
}




























